package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.DealDetailItem;
import com.kkd.kkdapp.utils.LogUtil;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SimpleAdapter extends BaseAdapter {

	LayoutInflater mInflater;
	List<DealDetailItem> mList;
	private static final Map<String, Integer> tradeTypeMap = new HashMap<String, Integer>();
	static {
		tradeTypeMap.put("3001", R.drawable.deal_type_icon_tou); // 投标扣款
		tradeTypeMap.put("3002", R.drawable.deal_type_icon_fang); // 借贷放款
		tradeTypeMap.put("3004", R.drawable.deal_type_icon_chong); // 充值
		tradeTypeMap.put("3005", R.drawable.deal_type_icon_qu); // 提现
		tradeTypeMap.put("3026", R.drawable.deal_type_icon_dong); // 解冻退款
		tradeTypeMap.put("3011", R.drawable.deal_type_icon_dong); // 解冻退款
		tradeTypeMap.put("3016", R.drawable.deal_type_icon_fan); // 提现失败返还
		tradeTypeMap.put("3018", R.drawable.deal_type_icon_yao); // 邀请好友理财奖励
		tradeTypeMap.put("3010", R.drawable.deal_type_icon_fei); // 提现手续费
		tradeTypeMap.put("3012", R.drawable.deal_type_icon_fei); // 服务费
		tradeTypeMap.put("3027", R.drawable.deal_type_icon_fei); // 管理费
		tradeTypeMap.put("3031", R.drawable.deal_type_icon_huan); // 还款扣款
		tradeTypeMap.put("3032", R.drawable.deal_type_icon_xi); // 还款利息
		tradeTypeMap.put("3033", R.drawable.deal_type_icon_xi); // 还款扣款利息
		tradeTypeMap.put("3034", R.drawable.deal_type_icon_bao); // 红包活动返现
		tradeTypeMap.put("3036", R.drawable.deal_type_icon_huan); // 还款
		tradeTypeMap.put("3043", R.drawable.deal_type_icon_guan); // 存钱罐收益
		tradeTypeMap.put("3039", R.drawable.deal_type_icon_bao); // 奖励红包
		tradeTypeMap.put("3044", R.drawable.deal_type_icon_fei); // 提现手续费返还
		tradeTypeMap.put("3045", R.drawable.deal_type_icon_ru); // 天天赚转入
		tradeTypeMap.put("3046", R.drawable.deal_type_icon_chu); // 天天赚转出
		tradeTypeMap.put("3049", R.drawable.deal_type_icon_cs); // 充值失败
		tradeTypeMap.put("3051", R.drawable.deal_type_icon_fx); // 现金返还
	}

	private static final DecimalFormat numDf = new DecimalFormat("#0.00");

	public SimpleAdapter(Context context, List<DealDetailItem> mList) {
		mInflater = LayoutInflater.from(context);
		this.mList = mList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemViewHolder holder;
		if (convertView != null
				&& convertView.getTag() instanceof ItemViewHolder) {
			holder = (ItemViewHolder) convertView.getTag();
		} else {
			holder = new ItemViewHolder();
			convertView = mInflater.inflate(R.layout.deal_detail_list_item,
					null);
			holder.mIcon = (ImageView) convertView.findViewById(R.id.icon);
			holder.mText = (TextView) convertView.findViewById(R.id.text);
			holder.mDate = (TextView) convertView.findViewById(R.id.date);
			holder.mValue = (TextView) convertView.findViewById(R.id.value);
			holder.mStatus = (TextView) convertView.findViewById(R.id.status);
			holder.profit = (TextView) convertView.findViewById(R.id.profit);
		}

		holder.profit.setVisibility(View.GONE);
		if ("3018".equals(mList.get(position).getTradeType())) {
			if (!TextUtils.isEmpty(mList.get(position).getExt())) {
				holder.profit.setVisibility(View.VISIBLE);
				holder.profit.setText(mList.get(position).getExt());
			}
		}
		// Integer imgRes =
		// tradeTypeMap.get(mList.get(position).getTradeType());
		// if (imgRes == null) {
		// holder.mIcon.setImageResource(R.drawable.deal_type_icon_default);
		// } else {
		// holder.mIcon.setImageResource(imgRes);
		// }
		holder.mText.setText(mList.get(position).getSummary());
		holder.mDate.setText(mList.get(position).getCreateTime()
				.replaceAll("-", "/"));
		if (mList.get(position).getType() == 0) {
			holder.mValue.setText("-"
					+ numDf.format(mList.get(position).getAmount()));
		} else if (mList.get(position).getType() == 1) {
			holder.mValue.setText("+"
					+ numDf.format(mList.get(position).getAmount()));
		}
		int status = mList.get(position).getStatus();
		String statusName = "";
		if ("3004".equals(mList.get(position).getTradeType())
				|| "3005".equals(mList.get(position).getTradeType())) {

			if (status == 0) {

				statusName = "未入账";

			} else if (status == 1) {

				statusName = "已入账";

			} else if (status == 2) {

				statusName = "入账失败";

			}

			statusName = (status == 1 ? "已入账" : "未入账");

		} else {

			statusName = "已入账";

		}

		holder.mStatus.setText(statusName);

		return convertView;

	}

	public class ItemViewHolder {
		public ImageView mIcon;
		public TextView mText;
		public TextView mDate;
		public TextView mValue;
		public TextView mStatus, profit;
	}
}
